﻿// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "GJ/Icon" {
Properties {
    [PerRendererData] _MainTex ("Main Texture", 2D) = "black" {}
    _BackgroundTex ("Background Texture", 2D) = "black" {}
    // mask
    _StencilComp("Stencil Comparison", Float) = 8
    _Stencil("Stencil ID", Float) = 0
    _StencilOp("Stencil Operation", Float) = 0
    _StencilWriteMask("Stencil Write Mask", Float) = 255
    _StencilReadMask("Stencil Read Mask", Float) = 255
    _ColorMask("Color Mask", Float) = 15
}
SubShader {
    Stencil
    {
        Ref[_Stencil]
        Comp[_StencilComp]
        Pass[_StencilOp]
        ReadMask[_StencilReadMask]
        WriteMask[_StencilWriteMask]
    }
    ColorMask[_ColorMask]

    Tags {
        "IgnoreProjector"="True"
        "Queue"="Transparent"
        "RenderType"="Transparent"
        "PreviewType"="Plane"
		"CanUseSpriteAtlas"="True"
    }
    Pass {
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off
        Lighting Off
        ZWrite Off
//        Ztest Always
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0

        #include "UnityCG.cginc"

        sampler2D _MainTex; float4 _MainTex_ST;
        sampler2D _BackgroundTex;

        struct appdata_t {
            float4 vertex : POSITION;
            fixed4 color : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        struct v2f {
            float4 vertex : SV_POSITION;
            fixed4 color : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_OUTPUT_STEREO
        };
        
        v2f vert (appdata_t v)
        {
            v2f o;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.color = v.color;
            o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            fixed4 tex = tex2D(_MainTex, i.texcoord);
            fixed4 background = tex2D(_BackgroundTex, i.texcoord);
            tex = tex * i.color;
            if (tex.a > 0) tex.a = i.color.a * background.a;
            return tex;
        }
        ENDCG
    }
}

}